package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MenuDao_f;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author fr
 * @date 2020-03-07 12:21
 */
@Service(interfaceClass = MenuService_f.class)
@Transactional
public class MenuServiceImpl_f implements MenuService_f {

    @Autowired
    private MenuDao_f menuDao_f;

    //查询所有一级菜单及条件查询
    public PageResult findMenu(QueryPageBean pageBean) {
        Integer currentPage = pageBean.getCurrentPage();
        Integer pageSize = pageBean.getPageSize();
        String queryString = pageBean.getQueryString();
        if (currentPage == null || "".equals(currentPage)) {
            currentPage = 1;
        }
        if (pageSize == null || "".equals(pageSize)) {
            pageSize = 10;
        }
        PageHelper.startPage(currentPage, pageSize);
        Page<Menu> menus = null;
        if (queryString == null) {
            //查询所有一级菜单
            menus = menuDao_f.findFirstMenu();
        } else {
            //条件查询
            menus = menuDao_f.findByCondition(queryString);
        }

        return new PageResult(menus.getTotal(),menus.getResult());
    }

    //查询所有菜单
    public List<Menu> findAll() {
        String s = null;
        Page<Menu> menus = menuDao_f.findByCondition(s);
        return menus;
    }

    //添加菜单项
    public void addMenu(Menu menu) {
        menuDao_f.addMenu(menu);
    }

    //编辑菜单项
    public void editMenu(Menu menu) {
        menuDao_f.editMeun(menu);
    }

    //查询单个
    public Menu findById(Integer id) {
        return menuDao_f.findById(id);
    }

    //删除菜单项
    public void delMenu(Integer id) {
        Integer count = menuDao_f.findCountById(id);
        if (count>0) {
            throw new RuntimeException(MessageConstant.DELETE_MENU_FAIL);
        }
        menuDao_f.delMenu(id);
    }

    //根据Id查询子菜单
    public List<Menu> findChild(Integer id) {
        List<Menu> childMenu = menuDao_f.findChild(id);
        return childMenu;
    }

}
